package com.example.h.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.EditText;
import com.example.h.entity.User;


/**
 * Created by H on 2017/12/12.
 */

public class UserDao {
    private SQLiteDatabase sdb;
    public UserDao(Context context){
        DbHelper dbHelper=DbHelper.getInstance(context);
        sdb=dbHelper.getReadableDatabase();
    }

    public User login(String name, String pwd){
        String sql="select * from User where u_name=?";
        Cursor cursor=sdb.rawQuery(sql,new String[]{name});
        while(cursor.moveToNext()){
            int uId=cursor.getInt(cursor.getColumnIndex("u_id"));
            String uName=cursor.getString(cursor.getColumnIndex("u_name"));
            String uPassword=cursor.getString(cursor.getColumnIndex("u_password"));
            if (uName.equals(name)&&pwd.equals(uPassword)) {
                return new User(uId, uName, null);
            }
        }
     return null;
    }

    public boolean addUser(User user){
        String sql="insert into User(u_name,u_password) values(?,?)";
        try{
            sdb.execSQL(sql,new String[]{user.getuName(),user.getuPassword()});
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return false;
    }
}
